package com.huilian.cloud.module.system.controller.admin.groupdatascope;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.groupdatascope.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.groupdatascope.GroupDatascopeDO;
import com.huilian.cloud.module.system.service.groupdatascope.GroupDatascopeService;

@Tag(name = "管理后台 - 用户组数据权限")
@RestController
@RequestMapping("/sys/group-datascope")
@Validated
public class GroupDatascopeController {

    @Resource
    private GroupDatascopeService groupDatascopeService;

    @PostMapping("/create")
    @Operation(summary = "创建用户组数据权限")
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:create')")
    public CommonResult<Integer> createGroupDatascope(@Valid @RequestBody GroupDatascopeSaveReqVO createReqVO) {
        return success(groupDatascopeService.createGroupDatascope(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户组数据权限")
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:update')")
    public CommonResult<Boolean> updateGroupDatascope(@Valid @RequestBody GroupDatascopeSaveReqVO updateReqVO) {
        groupDatascopeService.updateGroupDatascope(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户组数据权限")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:delete')")
    public CommonResult<Boolean> deleteGroupDatascope(@RequestParam("id") Integer id) {
        groupDatascopeService.deleteGroupDatascope(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户组数据权限")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:query')")
    public CommonResult<GroupDatascopeRespVO> getGroupDatascope(@RequestParam("id") Integer id) {
        GroupDatascopeDO groupDatascope = groupDatascopeService.getGroupDatascope(id);
        return success(BeanUtils.toBean(groupDatascope, GroupDatascopeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户组数据权限分页")
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:query')")
    public CommonResult<PageResult<GroupDatascopeRespVO>> getGroupDatascopePage(@Valid GroupDatascopePageReqVO pageReqVO) {
        PageResult<GroupDatascopeDO> pageResult = groupDatascopeService.getGroupDatascopePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GroupDatascopeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户组数据权限 Excel")
    @PreAuthorize("@ss.hasPermission('sys:group-datascope:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGroupDatascopeExcel(@Valid GroupDatascopePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GroupDatascopeDO> list = groupDatascopeService.getGroupDatascopePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户组数据权限.xls", "数据", GroupDatascopeRespVO.class,
                        BeanUtils.toBean(list, GroupDatascopeRespVO.class));
    }

}